import { _decorator, Component, Event, Node, Prefab, SkeletalAnimation, Sprite, SpriteFrame, UIOpacity, v3 } from 'cc';
import { MainUI } from './MainUI';
import { PoolManager } from 'db://assets/Scripts/Framework/Managers/PoolManager';
import { WeaponTypeUI } from './WeaponTypeUI';
import { WeaponItemUI } from './WeaponItemUI';
import { GameDataManager } from '../GameDataManager';
import { UIManager } from 'db://assets/Scripts/Framework/Managers/UIManager';
import { ProjectEventManager, ProjectEvent } from 'db://assets/Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;

@ccclass('WeaponUI')
export class WeaponUI extends Component {

    private static instance: WeaponUI;
    public static get Instance(): WeaponUI {
        return this.instance;
    }

    @property(Sprite)
    cameraBtn: Sprite;

    @property(Node)
    weaponLayout: Node;

    @property(Node)
    skinContent: Node;

    @property(Node)
    rtSprite: Node;

    @property(Prefab)
    weaponTypePrefab: Prefab;

    @property(Prefab)
    weaponItemPrefab: Prefab;

    @property([Sprite])
    performances: Sprite[] = [];

    @property([Node])
    typeBtns: Node[] = [];

    @property([SpriteFrame])
    cameraSfs: SpriteFrame[] = [];

    cameraGamer = false;
    type = 0;
    num = 0;

    protected onLoad(): void {
        WeaponUI.instance = this;
        MainUI.Instance._InitRotate(this.rtSprite);
    }

    protected onEnable(): void {
        this.SwitchWeaponType(0);
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    Back() {
        this.node.active = false;
        MainUI.Instance._InitPlayer();
        MainUI.Instance.PlayClick();
    }

    SwitchWeaponType(arg: Event | number) {
        if (arg instanceof Event) {
            let target: Node = arg.target;
            arg = target.parent.getSiblingIndex();
        }
        this.type = arg;
        for (let i = 0; i < this.typeBtns.length; i++) {
            const element = this.typeBtns[i].getComponent(UIOpacity);
            element.opacity = i == arg ? 255 : 0;
        }
        let datas = arg == 5 ? MainUI.Instance.items.Melee : MainUI.Instance.items.Gun[arg];
        for (let i = 0; i < datas.length; i++) {
            const element = datas[i];
            let weapon = this.weaponLayout.children[i];
            if (!weapon) weapon = PoolManager.GetNodeByPrefab(this.weaponTypePrefab, this.weaponLayout);
            let ui = weapon.getComponent(WeaponTypeUI);
            ui._Init(element);
            if (i == 0) ui.Click();
        }
        for (let i = datas.length; i < this.weaponLayout.children.length; i++) {
            const element = this.weaponLayout.children[i];
            PoolManager.PutNode(element);
        }
        let weapons = MainUI.Instance.weapons;
        for (let i = 0; i < weapons.children.length; i++) {
            const element = weapons.children[i];
            element.active = i == arg;
        }
        MainUI.Instance.camera.setRotationFromEuler(v3(0, this.cameraGamer ? 0 : 180));
        MainUI.Instance.PlayClick();
    }

    _SwitchWeapon(target: Node) {
        this.num = target.getSiblingIndex();
        let data = this.type == 5 ? MainUI.Instance.items.Melee[this.num] : MainUI.Instance.items.Gun[this.type][this.num];
        let item = this.skinContent.children[0].getComponent(WeaponItemUI);
        item._Init(data);
        let curWeapon = MainUI.Instance.curWeapon;
        let res = data.prefab;
        if (curWeapon.children[0]) PoolManager.PutNode(curWeapon.children[0]);
        PoolManager.GetNodeByBundle("MainPack", res, curWeapon);
        let str = "持刀待机";
        if (this.type == 0) str = "手枪待机";
        else if (this.type < 5) str = "持枪待机";
        let model = MainUI.Instance.model;
        model.getComponent(SkeletalAnimation).crossFade(str);
        let weapons = MainUI.Instance.weapons;
        for (let i = 0; i < weapons.children[this.type].children.length; i++) {
            const element = weapons.children[this.type].children[i];
            element.active = i == this.num;
        }
        this.performances[0].fillRange = (this.type == 5 ? data.damage : data.damage * data.bullet) / 100;
        this.performances[1].fillRange = data.rate / 5;
        this.performances[2].fillRange = Math.min(data.maxAmmo / 100, 1);
        this.performances[3].fillRange = data.armorExpose;
    }

    SwitchCamera() {
        this.cameraGamer = !this.cameraGamer;
        MainUI.Instance.camera.setRotationFromEuler(v3(0, this.cameraGamer ? 0 : 180));
        this.cameraBtn.spriteFrame = this.cameraSfs[this.cameraGamer ? 1 : 0];
        MainUI.Instance.PlayClick();
    }

    SetWeapon() {
        let data = GameDataManager.Instance.getArrayData("Weapon");
        data[0] = this.type;
        data[1] = this.num;
        GameDataManager.Instance.setArrayData("Weapon", data);
        UIManager.ShowTip("成功设置为主页展示");
        MainUI.Instance.PlayClick();
    }

}


